/*
 * TMP431.c
 *
 * Created: 2012-06-02 20:45:30
 *  Author: Sebastian_2
 */ 

#include "../Globals.h"
#include "TMP431.h"

#ifndef ATMEGA

void TMP431_Init() {
	uint8_t buffer[4];
	buffer[0] = TMP431_CONF1;
	buffer[1] = 0x04; // run continuously, extended temperature range
	buffer[2] = TMP431_CONV_RATE;
	buffer[3] = 0x04; // 1 conv/sec
	
	TWI_MasterWrite(&gTWI_Master, TMP431_ADDR, &buffer, 4);
}

void TMP431_ReadLocalTemp() {
	uint8_t buffer;
	buffer = TMP431_LOCAL_HI;
	TWI_MasterWriteRead(&gTWI_Master, TMP431_ADDR, &buffer, 1, 1);
	gLocalTemperature.integer = gTWI_Master.readData[0] - 64; // - 64 because of extended temp. range 
	
	buffer = TMP431_LOCAL_LO;
	TWI_MasterWriteRead(&gTWI_Master, TMP431_ADDR, &buffer, 1, 1);
	gLocalTemperature.fraction = gTWI_Master.readData[0] * 39; // (0.0625/16) * 10 000
	gLocalTemperature.fraction /= 100;
}

void TMP431_ReadRemoteTemp() {
	uint8_t buffer;
	buffer = TMP431_REMOTE_HI;
	TWI_MasterWriteRead(&gTWI_Master, TMP431_ADDR, &buffer, 1, 1);
	gRemoteTemperature.integer = gTWI_Master.readData[0] - 64; // - 64 because of extended temp. range 
	
	buffer = TMP431_REMOTE_LO;
	TWI_MasterWriteRead(&gTWI_Master, TMP431_ADDR, &buffer, 1, 1);
	gRemoteTemperature.fraction = gTWI_Master.readData[0] * 39; // (0.0625/16) * 10 000
	gRemoteTemperature.fraction /= 100;
}

#endif